package com.ias.agdis_r.activity.more;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.adapter.WifiAdapter;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.util.network.WifiUtil;

/**
 * WIFI热点过滤页面
 * @author zhangsc
 *
 */
@SuppressLint("NewApi")
public class WifiHotspotActivity extends BaseKeyActivity {

	private ListView list_wifi = null;
	private ImageView switch_wifi = null;
	private WifiAdapter wifiAdapter = null;
	private ActionBar actionBar;
	
	private WifiUtil wifiUtil;
	
	WifiManager wifiManager = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.activity_wifi_hostspot_list);
		actionBar = getActionBar();
		loadactionbar();
		wifiUtil = new WifiUtil(this);
		wifiAdapter = new WifiAdapter(this);
		list_wifi = (ListView)findViewById(R.id.list_wifi);
		list_wifi.setAdapter(wifiAdapter);
		switch_wifi = (ImageView) findViewById(R.id.switch_wifi);
		//设置按钮初始状态
		switch_wifi.setOnClickListener(wifiListenter);
		if(wifiUtil.isWifiEnabled())
		{
			switch_wifi.setBackgroundResource(R.drawable.wifi_on);
		}
		
		registerWifiBoradcastReceiver();
	}
	
	private OnClickListener wifiListenter = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(wifiUtil.isWifiEnabled()){
				wifiUtil.closeWifi();
				switch_wifi.setBackgroundResource(R.drawable.wifi_off);
			}
			else{
				wifiUtil.openWifi();
				switch_wifi.setBackgroundResource(R.drawable.wifi_on);
			}
		}
	};
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		wifiAdapter.updateUI();
		if(wifiUtil.isWifiEnabled())
		{
			switch_wifi.setBackgroundResource(R.drawable.wifi_on);
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mwifiBroadcastReceiver);
	}

	/**
	 * 加载ACTIONBAR
	 */
	private void loadactionbar() {
		// TODO Auto-generated method stub
		actionBar.setCustomView(LayoutInflater.from(WifiHotspotActivity.this)
				.inflate(R.layout.actionbar_back_and_home, null));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
		TextView title = (TextView) findViewById(R.id.txt_title);
		title.setText(R.string.WifiHotspotActivity);
		ImageView back = (ImageView) findViewById(R.id.action_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		ImageView home = (ImageView) findViewById(R.id.action_home);
		home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WifiHotspotActivity.this, MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
	}
	
	public void registerWifiBoradcastReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		registerReceiver(mwifiBroadcastReceiver, filter);
	}
	
	private BroadcastReceiver mwifiBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			
			
			if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
			{
				 NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);  
		         if(info.getState().equals(NetworkInfo.State.DISCONNECTED)){  
		            
		        	
		         }  
		         else if(info.getState().equals(NetworkInfo.State.CONNECTED)){  
		                  
		            WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);  
		            WifiInfo wifiInfo = wifiManager.getConnectionInfo();                   
		            //获取当前wifi名称  
		            System.out.println(getString(R.string.linkedNet) + wifiInfo.getSSID());
		         }  
		         wifiAdapter.updateUI();
			}
			else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否  
	            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);  
	            if(wifistate == WifiManager.WIFI_STATE_DISABLED){  
	                System.out.println(getString(R.string.systemCloseWifi));
	            }  
	            else if(wifistate == WifiManager.WIFI_STATE_ENABLED){  
	            	EffectManager.getInstance().ttsPlay(
							EffectManager.TTS_TYPE_UPTO, getString(R.string.wifiOn), false);
	            }  
	            wifiAdapter.updateUI();
	        }  

		}
	};
	/**
	 * 连接WIFI
	 */
	private void connectWifi(HashMap<String,Object> wifiMap,String pwd)
	{
		if(wifiMap!=null)
		{
			String ssid = (String) wifiMap.get(WifiUtil.TAG_SSID);
			String type = (String) wifiMap.get(WifiUtil.TAG_TYPE);
			
			WifiUtil wifiUtil = new WifiUtil(this);
			wifiUtil.connect(ssid,pwd,type);
		}
	}
}
